/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.web.save;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.shop.entity.goods.Goods;
import com.thinkgem.jeesite.modules.shop.service.goods.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.shop.entity.save.Save;
import com.thinkgem.jeesite.modules.shop.service.save.SaveService;

import java.util.List;
/**
 * 收藏表Controller
 * @author 唐君华
 * @version 2019-05-15
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/save/save")
public class SaveController extends BaseController {

	@Autowired
	private SaveService saveService;
	@Autowired
	private GoodsService gs;
	
	@ModelAttribute
	public Save get(@RequestParam(required=false) String id) {
		Save entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = saveService.get(id);
		}
		if (entity == null){
			entity = new Save();
		}
		return entity;
	}
	
	@RequiresPermissions("shop:save:save:view")
	@RequestMapping(value = {"list", ""})
	public String list(Save save, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Save> page = saveService.findPage(new Page<Save>(request, response), save); 
		model.addAttribute("page", page);

		List<Goods>  goodsList=gs.findList(new Goods());
		model.addAttribute("goodslist",goodsList);
		System.out.println(goodsList);

		return "modules/shop/save/saveList";
	}

	@RequiresPermissions("shop:save:save:view")
	@RequestMapping(value = "form")
	public String form(Save save, Model model) {
		model.addAttribute("save", save);
		List<Goods>  goodsList=gs.findList(new Goods());
		model.addAttribute("goodslist",goodsList);
		return "modules/shop/save/saveForm";
	}

	@RequiresPermissions("shop:save:save:edit")
	@RequestMapping(value = "save")
	public String save(Save save, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, save)){
			return form(save, model);
		}

		saveService.save(save);
		addMessage(redirectAttributes, "保存收藏成功");
		return "redirect:"+Global.getAdminPath()+"/shop/save/save/?repage";
	}


	@RequiresPermissions("shop:save:save:edit")
	@RequestMapping(value = "delete")
	public String delete(Save save, RedirectAttributes redirectAttributes) {
		saveService.delete(save);
		addMessage(redirectAttributes, "删除收藏成功");
		return "redirect:"+Global.getAdminPath()+"/shop/save/save/?repage";
	}

}